package org.aphelie.torg.ruler.internal.ui.editor.character.validation;

import org.eclipse.core.resources.IProject;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.IManagedForm;
import org.omg.CORBA.INITIALIZE;

public abstract class TextValidator extends AbstractControlValidator {

    private ModifyListener fModifyListener;

    private boolean fAutoValidate;

    private String fCurrentText;
	
	public TextValidator(IManagedForm managedForm, Text control, IProject project,
			boolean autoValidate) {
		super(managedForm, control, project);
		fAutoValidate = autoValidate;
		initialize();
	}
	
	private void initialize() {
		// Save the current contents of the Text field
		fCurrentText = getText().getText();
		// No listeners required if auto-validation is off
		if (fAutoValidate == false) {
			return;
		}
		// Create the listeners for auto-validation
		createListeners();
		// Add the listeners if the validator is enabled
		if (getEnabled()) {
		addListeners();
		}
	}

	   /* (non-Javadoc)
     * @see org.eclipse.pde.internal.ui.editor.validation.AbstractControlValidator#setEnabled(boolean)
     */
    public void setEnabled(boolean enabled) {
        // Nothing to do here if enablement is not being changed
        if (getEnabled() == enabled) {
            return;
        }
        // Update validator enablement
        super.setEnabled(enabled);
        // No listeners required if auto-validation is off
        if (fAutoValidate == false) {
            return;
        }
        // Add listeners if enabled; otherwise, remove them
        if (getEnabled()) {
            addListeners();
        } else {
            removeListeners();
        }
    }

    protected void createListeners() {
        fModifyListener = new ModifyListener() {
            public void modifyText(ModifyEvent e) {
                handleModifyTextEvent(e);
            }
        };
    }

    protected void handleModifyTextEvent(ModifyEvent e) {
        // Validation is not required if the current text contents is the 
        // same as the new text contents
        String newText = getText().getText();
        if (newText.equals(fCurrentText)) {
            return;
        }
        // Save the current contents of the Text field
        fCurrentText = newText;
        // Perform auto-validation
        validate();
    }

    protected void addListeners() {
        getText().addModifyListener(fModifyListener);
    }

    protected void removeListeners() {
        getText().removeModifyListener(fModifyListener);
    }

    protected Text getText() {
        return (Text) getControl();
    }

    /* (non-Javadoc)
     * @see org.eclipse.pde.internal.ui.editor.validation.AbstractControlValidator#autoEnable()
     */
    protected boolean autoEnable() {
        // Enable validator if the text field is editable
        if (getText().getEditable() == false) {
            return false;
        }
        return super.autoEnable();
    }
}
